﻿using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

[System.Serializable]
public class SwitchEvent : UnityEvent<bool>
{
}

public class UISwitch : MonoBehaviour, IPointerClickHandler
{
    //默认状态是开还是关
    [SerializeField]
    private bool _isOn = true;
    public bool IsOn
    {
        get => _isOn;
        set
        {
            Debug.Log(value);
            _isOn = value;
            updateSprite();
        }
    }
    
    [Header("关掉时的Sprite")][Tooltip("关掉时的Sprite")]
    public GameObject offSprite;
    [Tooltip("打开时的Sprite")]
    public GameObject onSprite;
    public SwitchEvent eventListener;
    void Start()
    {
        if (offSprite == null) offSprite = transform.Find("OffSprite").gameObject;
        if (onSprite == null) onSprite = transform.Find("OnSprite").gameObject;
        updateSprite();
    }

    
    void updateSprite()
    {
        offSprite.SetActive(!_isOn);
        onSprite.SetActive(_isOn);
    }
    
    public void OnPointerClick(PointerEventData eventData)
    {
        IsOn = !IsOn;
        eventListener.Invoke(_isOn);
    }
}